package com.ljt.framework.test;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.times;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.ljt.framework.test.demo.ConstructorService;
import com.ljt.framework.test.demo.UserAction;

/**
 * @Project       : framework-test
 * @Program Name  : com.ljt.framework.test.MockForConstructorDemo.java
 * @Description   : 在测试中 模拟构造函数
 * @Author        : wangchao
 * @Creation Date : 2016年4月14日 下午12:13:07 
 * @ModificationHistory  
 * Who          When             What 
 * ----------   -------------    -----------------------------------
 * wangchao     2016年4月14日        create
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest({ConstructorService.class,UserAction.class})
public class MockForConstructorDemo {
	
	@Test
	public void test() throws Exception{
		UserAction userAction = new UserAction();
		
		// 模拟构造方法
		// value必须提前构造
		ConstructorService value = new ConstructorService("mock");
		PowerMockito.spy(ConstructorService.class);
		
		// 模拟构造函数
		PowerMockito.whenNew(ConstructorService.class).withArguments(anyString()).thenReturn(value);
		// 执行业务逻辑
		userAction.executeForConstructor("real");
		// 验证构造方法
		PowerMockito.verifyNew(ConstructorService.class,times(1)).withArguments(anyString());
	}

}
